<?php
/**
 * @author wanghui <whlives@qq.com>
 * @version 1.0，20160202
 * @package 快递模板处理
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Express_templet_model extends CI_Model
{
    //数据转换
    public function conver($express_config,$order_id)
    {
        $result = array(); //函数返回数据
        $whole_data   = array(); //实际的数据

        //获取订单信息
        $this->load->model('loop_model');
        $order_data = $this->loop_model->get_id('order', $order_id);

        $order_sku_data = $this->loop_model->get_list('order_sku', array('where'=>array('order_id'=>$order_id)));
        $all_sku_weight = 0;
        if (!empty($order_sku_data)) {
            foreach ($order_sku_data as $key) {
                $all_sku_weight = $all_sku_weight+$key['sku_weight'];
            }
        }

        /*拼接实际数据 $wholeData*/

        //查询收货人地域数据
        $this->load->model('areas_model');
        $area_name = $this->areas_model->get_name(array($order_data['prov'], $order_data['city'], $order_data['area']));

        $whole_data['ship_name']   = $order_data['full_name'];
        $whole_data['ship_area_0'] = isset($area_name[$order_data['prov']]) ? $area_name[$order_data['prov']] : '';
        $whole_data['ship_area_1'] = isset($area_name[$order_data['city']]) ? $area_name[$order_data['city']] : '';
        $whole_data['ship_area_2'] = isset($area_name[$order_data['area']]) ? $area_name[$order_data['area']] : '';
        $whole_data['ship_addr']   = $order_data['address'];
        $whole_data['ship_tel']    = $order_data['tel'];

        $whole_data['ship_detail_addr'] = $area_name[$order_data['prov']].$area_name[$order_data['city']].$area_name[$order_data['area']].$order_data['address'];

        //查询发货人信息
        $shop_address = $this->loop_model->get_where('member_shop_address', array('shop_id'=>$order_data['shop_id'],'is_default'=>1));
        $shop_area_name = $this->areas_model->get_name(array($shop_address['prov'], $shop_address['city'], $shop_address['area']));

        $whole_data['dly_name']    = $shop_address['full_name'];
        $whole_data['dly_area_0']  = isset($shop_area_name[$shop_address['prov']]) ? $shop_area_name[$shop_address['prov']] : '';
        $whole_data['dly_area_1']  = isset($shop_area_name[$shop_address['city']]) ? $shop_area_name[$shop_address['city']] : '';
        $whole_data['dly_area_2']  = isset($shop_area_name[$shop_address['area']]) ? $shop_area_name[$shop_address['area']] : '';
        $whole_data['dly_address'] = $shop_address['address'];
        $whole_data['dly_tel']     = $shop_address['tel'];

        $whole_data['date_y']      = date('Y');
        $whole_data['date_m']      = date('m');
        $whole_data['date_d']      = date('d');

        $whole_data['order_id']    = $order_data['order_no'];
        $whole_data['order_price'] = format_price($order_data['order_price']);
        $whole_data['order_weight'] = $all_sku_weight;
        $whole_data['order_count']  = count($order_sku_data);
        $whole_data['order_memo']   = $order_data['m_desc'];
        $whole_data['ship_time']    = $order_data['delivery_time'];
        $whole_data['shop_name']    = isset($site_config['website_title']) ? $site_config['website_title'] : '';
        $whole_data['tick']         = '√';

        //进行数据替换
        foreach($express_config as $key => $val)
        {
            $item_tmp             = json_decode($val, true);
            $item_tmp['typeText'] = isset($whole_data[$item_tmp['typeId']]) ? $whole_data[$item_tmp['typeId']] : '';
            $result[]        = ch_json_encode($item_tmp);
        }

        return $result;
    }

    //获取配置信息
    public function get_config(){
        $item_data = array(
            'ship_name'=>'收货人-姓名',
            'ship_area_0'=>'收货人-地区1级',
            'ship_area_1'=>'收货人-地区2级',
            'ship_area_2'=>'收货人-地区3级',
            'ship_addr'=>'收货人-地址',
            'ship_tel'=>'收货人-电话',
            'ship_detail_addr'=>'收货人-地区+详细地址',
            'dly_name'=>'发货人-姓名',
            'dly_area_0'=>'发货人-地区1级',
            'dly_area_1'=>'发货人-地区2级',
            'dly_area_2'=>'发货人-地区3级',
            'dly_address'=>'发货人-地址',
            'dly_tel'=>'发货人-电话',
            'date_y'=>'当前日期-年',
            'date_m'=>'当前日期-月',
            'date_d'=>'当前日期-日',
            'order_id'=>'订单-订单号',
            'order_price'=>'订单总金额',
            'order_weight'=>'订单物品总重量',
            'order_count'=>'订单-物品数量',
            'order_memo'=>'订单-备注',
            'ship_time'=>'订单-送货时间',
            'shop_name'=>'网店名称',
            'tick'=>'√ - 对号',
        );
        return $item_data;
    }
}
